/*
 * @Description:
 * @Version: 2.0
 * @Autor: snzo.chen
 * @Date: 2023-11-16 11:14:17
 * @LastEditors: snzo.chen
 * @LastEditTime: 2023-12-20 14:23:08
 */
import { type UserConfig, type ConfigEnv, loadEnv } from 'vite';
import vue from '@vitejs/plugin-vue';
import { resolve } from 'node:path';
import Components from 'unplugin-vue-components/vite';
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons';
import { AntDesignVueResolver } from 'unplugin-vue-components/resolvers';
import checker from 'vite-plugin-checker';
import dayjs from 'dayjs';
import pkg from './package.json';

const CWD = process.cwd();

const __APP_INFO__ = {
  pkg,
  lastBuildTime: dayjs().format('YYYY-MM-DD HH:mm:ss'),
};

// https://vitejs.dev/config/
export default ({ command, mode }: ConfigEnv): UserConfig => {
  const { VITE_BASE_URL, VITE_DROP_CONSOLE } = loadEnv(mode, CWD);

  const isBuild = command === 'build';
  return {
    base: VITE_BASE_URL,
    server: {
      host: 'localhost',
      port: 8005,
      open: true,
      proxy: {
        '/jjzxbm': {
          target: 'https://testcontest.snzo.cn',
          changeOrigin: true,
          // rewrite: (path) => path.replace(/^\/api/, ""),
        },
        '/local': {
          target: 'http://192.168.0.27:8080',
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/local/, ''),
        },
      },
    },
    resolve: {
      alias: [
        {
          find: '@',
          replacement: resolve(__dirname, './src'),
        },
      ],
    },
    css: {
      // css预处理器
      preprocessorOptions: {
        less: {
          charset: false,
          additionalData: '@import "./src/assets/styles/global.less";',
        },
      },
    },
    plugins: [
      vue(),
      createSvgIconsPlugin({
        // Specify the icon folder to be cached
        iconDirs: [resolve(CWD, 'src/assets/icons')],
        // Specify symbolId format
        symbolId: 'svg-icon-[dir]-[name]',
      }),
      Components({
        resolvers: [
          AntDesignVueResolver({
            importStyle: false,
            // resolveIcons: true, // css in js
          }),
        ],
      }),
      checker({
        typescript: true,
        vueTsc: true,
        eslint: {
          lintCommand: 'eslint "./src/**/*.{.vue,ts,tsx}"', // for example, lint .ts & .tsx
        },
      }),
    ],
    esbuild: {
      pure: VITE_DROP_CONSOLE ? ['console.log', 'debugger'] : [],
      supported: {
        // https://github.com/vitejs/vite/pull/8665
        'top-level-await': true,
      },
    },
    build: {
      outDir: 'dist/vueTicketAdmin',
    },
  };
};
